package com.jtech.together.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.bmob.v3.BmobObject;
import cn.bmob.v3.datatype.BmobGeoPoint;
import cn.bmob.v3.datatype.BmobRelation;

/**
 * 动态表
 * Created by wuxubaiyang on 2017/5/11.
 */

public class TrendsModel extends BmobObject {
    private UserModel user = new UserModel();//发表用户
    private String content = "";//发表文本内容
    private List<String> images = new ArrayList<>();//图片集合
    private BmobGeoPoint location;//位置信息
    private String address = "";//位置信息描述
    private BmobRelation like = new BmobRelation();//点赞关联关系
    private BmobRelation unlike = new BmobRelation();//踩关联关系
    private Integer likeCount = 0;//喜欢数
    private Integer unlikeCount = 0;//踩数
    private List<String> likes = new ArrayList<>();//赞的用户id
    private List<String> unlikes = new ArrayList<>();//踩的用户id
    private Integer commentCount = 0;//评论数

    public TrendsModel(String objectId) {
        setObjectId(objectId);
    }

    public TrendsModel() {
    }

    /**
     * 增加评论数
     */
    public TrendsModel addCommentCount() {
        increment("commentCount");
        return this;
    }

    /**
     * 赞
     *
     * @param userModel
     */
    public TrendsModel like(UserModel userModel, int count) {
        String userId = userModel.getObjectId();
        //操作关系
        setLike(getRelation(userModel, count > 0));
        //操作赞数
        increment("likeCount", count);
        //操作赞的人
        if (count > 0) {
            add("likes", userId);
        } else {
            removeAll("likes", Arrays.asList(userId));
        }
        return this;
    }

    /**
     * 不喜欢
     *
     * @param userModel
     */
    public TrendsModel unlike(UserModel userModel, int count) {
        String userId = userModel.getObjectId();
        //操作关系
        setUnlike(getRelation(userModel, count > 0));
        //操作赞数
        increment("unlikeCount", count);
        //操作赞的人
        if (count > 0) {
            add("unlikes", userId);
        } else {
            removeAll("unlikes", Arrays.asList(userId));
        }
        return this;
    }


    /**
     * 获取关联关系
     *
     * @param object
     * @param isAdd
     * @return
     */
    private BmobRelation getRelation(BmobObject object, boolean isAdd) {
        BmobRelation relation = new BmobRelation();
        if (isAdd) {
            relation.add(object);
        } else {
            relation.remove(object);
        }
        return relation;
    }

    public UserModel getUser() {
        return user;
    }

    public void setUser(UserModel user) {
        this.user = user;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public List<String> getImages() {
        return images;
    }

    public void setImages(List<String> images) {
        this.images = images;
    }

    public BmobGeoPoint getLocation() {
        return location;
    }

    public void setLocation(BmobGeoPoint location) {
        this.location = location;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public BmobRelation getLike() {
        return like;
    }

    public void setLike(BmobRelation like) {
        this.like = like;
    }

    public BmobRelation getUnlike() {
        return unlike;
    }

    public void setUnlike(BmobRelation unlike) {
        this.unlike = unlike;
    }

    public Integer getLikeCount() {
        return likeCount;
    }

    public void setLikeCount(Integer likeCount) {
        this.likeCount = likeCount;
    }

    public Integer getUnlikeCount() {
        return unlikeCount;
    }

    public void setUnlikeCount(Integer unlikeCount) {
        this.unlikeCount = unlikeCount;
    }

    public List<String> getLikes() {
        return likes;
    }

    public void setLikes(List<String> likes) {
        this.likes = likes;
    }

    public List<String> getUnlikes() {
        return unlikes;
    }

    public void setUnlikes(List<String> unlikes) {
        this.unlikes = unlikes;
    }

    public Integer getCommentCount() {
        return commentCount;
    }

    public void setCommentCount(Integer commentCount) {
        this.commentCount = commentCount;
    }
}